<%#
name: kickstart_network_interface
model: ProvisioningTemplate
snippet: true
model: ProvisioningTemplate
kind: snippet
description: |
  Generates network directive for a given interface. It is not expected to be used directly.
-%>
<%#
  # Variables: iface, host, static, static6
-%>
<%if @iface.managed? -%>
<%
network_options = []
nameservers = []
subnet4 = @iface.subnet
subnet6 = @iface.subnet6

rhel_compatible = @host.operatingsystem.family == 'Redhat' && @host.operatingsystem.name != 'Fedora'
is_fedora = @host.operatingsystem.name == 'Fedora'
os_major = @host.operatingsystem.major.to_i

# device and hostname
if @iface.bond? || @iface.bridge?
  network_options.push("--device=#{@iface.identifier}")
else
  network_options.push("--device=#{@iface.mac || @iface.identifier}")
end
network_options.push("--hostname #{@host.name}")

# single stack
network_options.push("--noipv4") unless subnet4
network_options.push("--noipv6") unless subnet6
network_options.push("--onboot=false") unless subnet4 || subnet6 || @iface.bond?

# dual stack MTU check
raise("IPv4 and IPv6 subnets have different MTU") if subnet4 && subnet6 && subnet4.mtu.present? && subnet6.mtu.present? && subnet4.mtu != subnet6.mtu

# mtu method is taking into account both ipv4 and ipv6 stacks
if @iface.respond_to?(:mtu) && @iface.mtu
  network_options.push("--mtu=#{@iface.mtu}")
end

# IPv4
if subnet4
  if !subnet4.dhcp_boot_mode? || @static
    network_options.push("--bootproto static")
    network_options.push("--ip=#{@iface.ip}")
    network_options.push("--netmask=#{subnet4.mask}")
    network_options.push("--gateway=#{subnet4.gateway}")
  elsif subnet4.dhcp_boot_mode?
    network_options.push("--bootproto dhcp")
  end

  nameservers.concat(subnet4.dns_servers)
end

# IPv6
if subnet6
  if !subnet6.dhcp_boot_mode? || @static6
    network_options.push("--ipv6=#{@iface.ip6}/#{subnet6.cidr}")
    network_options.push("--ipv6gateway=#{subnet6.gateway}")
  elsif subnet6.dhcp_boot_mode?
    network_options.push("--ipv6 auto")
  end

  nameservers.concat(subnet6.dns_servers)
end

# bond
if @iface.bond?
  bond_slaves = @iface.attached_devices_identifiers.join(',')
  network_options.push("--bondslaves=#{bond_slaves}")
  network_options.push("--bondopts=mode=#{@iface.mode},#{@iface.bond_options.tr(' ', ',')}")
end

# bridge
if @iface.bridge?
  bridge_slaves = @iface.attached_devices_identifiers.join(',')
  network_options.push("--bridgeslaves=#{bridge_slaves}")
  # Currently no support for bridge options in the interface.
end

# VLAN (only on physical is recognized)
if @iface.virtual? && @iface.tag.present? && @iface.attached_to.present?
  network_options.push("--vlanid=#{@iface.tag}")
  network_options.push("--interfacename=vlan#{@iface.tag}") if (is_fedora && os_major >= 21) || (rhel_compatible && os_major >= 7)
end

# DNS
if nameservers.empty?
  network_options.push("--nodns")
else
  network_options.push("--nameserver=#{nameservers.join(',')}")
end

# Search domain - available from Fedora 39 (RHEL 10)
if @iface.domain && ((is_fedora && os_major >= 39) || (rhel_compatible && os_major >= 10))
  network_options.push("--ipv4-dns-search=#{@iface.domain}") if subnet4
  network_options.push("--ipv6-dns-search=#{@iface.domain}") if subnet6
end
-%>
<%= "network #{network_options.join(' ')}\n" -%>
<% end -%>
